const Model = require("../../models");
const { APPLY_STATUS } = require("../../sys_config");

module.exports = {
  name: "applys",

  async get_applys(query) {
    try {
      const model = Model.models.applys;
      const res = await model.get_applys(query);
      return { ok: 1, data: res };
    } catch (e) {
      console.log(e);
      return { ok: 0 };
    }
  },

  async get_apply_cal(d_id) {
    try {
      const model = Model.models.applys.core;
      const all = await model.count({ where: { d_id } });
      const receved = await model.count({
        where: { d_id, status: APPLY_STATUS.RESOVED },
      });
      const unrecev = await model.count({
        where: { d_id, status: APPLY_STATUS.PENDING },
      });
      const timeout = all - receved - unrecev;
      return { ok: 1, data: { all, unrecev, receved, timeout } };
    } catch (e) {
      console.log(e);
      return { ok: 0 };
    }
  },

  async add(data) {
    try {
      const model = Model.models.applys.core;
      const res = await model.create(data);
      return { ok: 1, data: res };
    } catch (e) {
      console.log(e);
      return { ok: 0 };
    }
  },

  async update({ id, ...data }) {
    try {
      await Model.models.applys.core.update(data, {
        where: { id },
      });
      return { ok: 1 };
    } catch (e) {
      console.log(e);
      return { ok: 0 };
    }
  },

  async del_apply(query) {
    try {
      await Model.models.applys.core.destroy({ where: query });
      return { ok: 1 };
    } catch (e) {
      console.log(e);
      return { ok: 0 };
    }
  },
};
